package com.puppet.building182.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.puppet.building182.config.MinioConfig;
import com.puppet.building182.handler.exception.MyBuildingException;
import com.puppet.building182.pojo.Permission;
import com.puppet.building182.pojo.Result;
import com.puppet.building182.pojo.User;
import com.puppet.building182.pojo.enums.BuildingCode;
import com.puppet.building182.service.PermissionService;
import com.puppet.building182.utils.MinioUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/{id}")
    public Result<?> getPermission(@PathVariable("id") Integer id) {
        return Result.ok(permissionService.getPermission(id));
    }

    @PostMapping("/{id}")
    public Result<?> savePermission(@PathVariable("id") Integer id, @RequestBody List<Integer> ids) {
        return Result.ok(permissionService.savePermission(id, ids), BuildingCode.SAVE_SUCCESS.getMessage());
    }


    //正式页面呈现
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<?> getAllPermission(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Permission> list = permissionService.getAllPermission();
        PageInfo<Permission> pageInfo = new PageInfo<>(list);
        return Result.ok(pageInfo);
    }

    @DeleteMapping("/{id}")
    public Result<?> deletePermission(@PathVariable("id") Integer id) {
        return Result.ok(permissionService.deletePerms(id), BuildingCode.DELETE_SUCCESS.getMessage());
    }

    @GetMapping("/getById/{id}")
    public Result<?> getPermissionById(@PathVariable("id") Integer id) {
        return Result.ok(permissionService.getById(id));
    }

    @PutMapping
    public Result<?> updatePermission(@RequestBody Permission permissions) {
        return Result.ok(permissionService.updatePermission(permissions));
    }

    @PostMapping("/upload")
    public Result<?> uploadPermission(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Permission.class, new PageReadListener<Permission>(data -> {
            permissionService.addPermsByFile(data);
        })).sheet().doRead();
        return Result.ok(null, BuildingCode.SAVE_SUCCESS.getMessage());
    }
@Autowired
private MinioUtils minioUtils;
    @GetMapping("/downloadModel/{PermModel}")
    public ResponseEntity<byte[]> downloadModel(@PathVariable("PermModel") String PermModel) {
        return minioUtils.download(PermModel);

    }
//    @GetMapping("/downloadModel/{PermModel}")
//    public ResponseEntity<byte[]> downloadPermModel(@PathVariable String permModel) {
//        try {
//            ResponseEntity<byte[]> fileResponse = minioUtils.download(permModel);
//
//            // 添加自定义响应头
//            HttpHeaders headers = new HttpHeaders();
//            headers.add("X-Download-Filename", permModel);
//            headers.add("X-Download-Message", "文件下载成功");
//            headers.add("Access-Control-Expose-Headers", "X-Download-Filename, X-Download-Message");
//
//            return ResponseEntity.ok()
//                    .headers(headers)
//                    .body(fileResponse.getBody());
//
//        } catch (Exception e) {
//            throw new MyBuildingException(Result.fail("文件下载失败: " + e.getMessage()));
//        }
//    }
    @GetMapping("/getAllMenuPerms")
    public Result<?> getAllMenuPerms(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Permission> allMenuPerms=permissionService.getAllPermission(user.getId());
        return Result.ok(allMenuPerms);
    }

}
